$(function() {
	//等待加载
	loading();
	var aId = getQueryString("aId");
	ajaxSelectUserBankCards(aId);
	ajaxGetPayPswStatus(getPayPswStatus);
	$("#money").val("");
	$("#payPsw").val("");
	$("#accUsername").val("");
	$("#bankName").val("");
	$("#cardNum").val("");
	var user=getUser_C();
	if(user){
		$("#balance").val(user.balance);
		$(".sureBtn").click(function() {
			var money = $("#money").val();
			if($("#accUsername").val()==null||$("#accUsername").val()==""||$("#bankName").val()==null||$("#bankName").val()==""||$("#cardNum").val()==null||$("#cardNum").val()==""){
				alertBox("请选择一张银行卡",'');
			}else if($("#money").val() < 100||$("#money").val()==null||$("#money").val()==""){
				alertBox("请按照要求输入金额",'');
			}else if($("#payPsw").val==null||$("#payPsw").val()==""){
				alertBox("请输入支付密码",'');
			}else if((user.balance - money) < 0){
				alertBox("余额不足",'');
			}
			else{
				/**
				 * 验证输入的支付密码是否正确
				 */
				ajaxValidateOldPayPsw($("#payPsw").val(), ajaxTransOutToBankCardExecute);
			}

    	});
    }
});
/**
 * 加载转入银行卡页面所需的  用户账户（持有的银行卡）详情
 * @param aId
 */
function ajaxSelectUserBankCards(aId){
	var url="/Lvscore_User_M/adScore/ajaxSelectUserBankCards";
	$.post(
		url,
		function(data) {
			if (data.length > 0) {
				$("#bankInfo").empty();
				$.each(data, function(i, e) {
					$("#bankInfo").append(
						'<li>' +
							'<a href="javascript:void(0)">' +
								'<input type="radio" onclick="chooseAcc(' + e.userAccount.id + ')" name="bank" id="bankCode_'+e.userAccount.id+'"/> ' +
								'<span class="bankName"><label for="bankCode_'+e.userAccount.id+'">' + e.userAccount.bankName + '</label></span>' +
								'<span class="cardNo">尾号' + e.showAccountNum + '</span>' +
							'</a>' +
						'</li>' +
						'<input type="hidden" id="username_' + e.userAccount.id + '" value="' + e.user.name + '"/>' +
						'<input type="hidden" id="accUsername_' + e.userAccount.id + '" value="' + e.userAccount.accountUserName + '"/>' +
						'<input type="hidden" id="bankname_' + e.userAccount.id + '" value="' + e.userAccount.bankName + '"/>' +
						'<input type="hidden" id="cardnum_' + e.userAccount.id + '" value="' + e.userAccount.accountNum + '"/>');
				});
				$("#bankInfo").append('<p class="btnSure btnBox handes" onclick="popupClose()" style="margin:0px 90px;position:absolute;bottom:15px;">确定</p>');
				if (aId) {
					$("#bankCode_"+aId).click();
					chooseAcc(aId);
				} else {
					$("#bankCode_"+data[0].userAccount.id).click();
					chooseAcc(data[0].userAccount.id);
				}
			} else {
				$("#bankInfo").css("border","none").html('<p class="noResult"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">您并未绑定银行卡</p>');
			}
		},
		"json"
	);
	//等待加载的效果消失
	$(".loadingBox").hide();
}
function chooseAcc(id) {
	$("#aId").val(id);
	$("#money").val("");
	$("#accUsername").val($("#accUsername_" + id).val());
	$("[name='toUsername']").text($("#username_" + id).val());
	$("#bankName").val($("#bankname_" + id).val());
	$("#cardNum").val($("#cardnum_" + id).val());
	var num = $("#cardnum_" + id).val();
	num = num.substring(num.length - 4, num.length);
	$("[name='card_num']").text(num);
}
/**
 * 计算提现所需服务费
 * @param aId
 * @param money
 */
function getServiceCost(aId, money) {
	var m = 0;
	/*if (money % 10 == 0) {
		m = money / 10;
	} else {
		m = money / 10 + 1;
	}*/
	return m;
}

/**
 * 支付密码正确之后   确认提现或转入银行卡
 * @param accountId		账户id（对应一张银行卡）
 * @param outBalance	提现（转出）金额
 * @param serviceCost	服务费
 */
function ajaxTransOutToBankCardExecute(){
	var aId = $("#aId").val();
	var money = $("#money").val();
	var serviceCost = getServiceCost(aId, money);
	$("[name='toMoney']").text(money);
	var url="/Lvscore_User_M/adScore/ajaxTransOutToBankCardExecute";
	$.post(
		url,
		{
			'accountId' : aId,
			'outBalance' : money,
			'serviceCost' : serviceCost
		},
		function(data){
			if (data == 1) {
				flushUser();
				$(".transferSuccess").show();
			} else if (data == -1) {
				$(".transferFail").show();
			} else {
				alert('服务器异常');
			}
			//待加载的效果消失
			$(".loadingBox").hide();
		},
		"json"
	);
}

function hideTransferBox() {
	$(".transferSuccess").hide();
	$(".transferFail").hide();
}

/**
 * 我的银行卡弹窗弹出和关闭
 */
function popup(){
	var display =$('.bankList').css('display');
	if(display == 'none'){
		$(".bankList").show();
	}else {
		popupClose();
	}

}
function popupClose(){
	$(".bankList").hide();
}

function getPayPswStatus(status) {
	if (status) {
		$("#paymentStatus").html('<a href="javascript:void(0)" onclick="forgotPayPsw()" class="forgetpsd">忘记支付密码？</a>');
	} else {
		$(".mask").show();
		$(".confirmBox").show();
		$("#paymentStatus").html('<a href="javascript:void(0)" onclick="setPayPsw()" class="forgetpsd">设置支付密码？</a>');
	}
}